package com.linked.basemanage.provideinterfaces;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.basemanage.config.RedisKeysConfig;
import com.linked.basemanage.deletebak.bean.po.DeleteBakPO;
import com.linked.basemanage.deletebak.service.IDeleteBakService;
import com.linked.basemanage.generallog.bean.po.LogInfoPO;
import com.linked.basemanage.generallog.service.IGeneralLogService;
import com.linked.basemanage.pass.service.IPassHandleService;
import com.linked.basemanage.switchsetting.service.ISwitchSettingService;
import com.linked.basemanage.util.HandleRedisUtils;
import com.linked.commonentity.basemanage.deletebak.LinkedDeleteBak;
import com.linked.universal.api.BaseManageAPI;
import com.linked.commonentity.basemanage.generallog.LinkedGeneralLog;
import com.linked.universal.common.LinkedResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/feign")
public class BaseManageFeignController implements BaseManageAPI {


    private final ISwitchSettingService switchSettingService;

    private final IGeneralLogService generalLogService;

    private final ObjectMapper mapper;

    private final IPassHandleService passHandleService;

    private final HandleRedisUtils redisUtils;

    private final IDeleteBakService deleteBakService;

    public BaseManageFeignController(ISwitchSettingService switchSettingService,
                                     IGeneralLogService generalLogService, ObjectMapper mapper,
                                     IPassHandleService passHandleService, HandleRedisUtils redisUtils,
                                     IDeleteBakService deleteBakService
    ) {
        this.switchSettingService = switchSettingService;
        this.generalLogService = generalLogService;
        this.mapper = mapper;
        this.passHandleService = passHandleService;
        this.redisUtils = redisUtils;
        this.deleteBakService = deleteBakService;
    }

    /**
     * 查询开关状态
     * 返回值：true;false;null
     * null表示没有配置开关！
     */
    @Override
    @PostMapping("/findSwitchStatus")
    public Boolean findSwitchStatus(String switchCode) {
        log.info("switchCode:" + switchCode);
        if (StringUtils.isEmpty(switchCode)) {
            return null;
        }
        return switchSettingService.findSwitchStatus(switchCode);
    }

    @Override
    @PostMapping("/turnSwitch")
    public Boolean turnSwitch(String switchCode) {
        log.info("转动开关接口 ----- switchCode:" + switchCode);
        if (StringUtils.isEmpty(switchCode)) {
            return null;
        }
        return switchSettingService.turnSwitch(switchCode);
    }

    @PostMapping("/insertGeneralLog")
    @Override
    public void insertGeneralLog(@RequestBody LinkedGeneralLog logInfo) {
        try {
            generalLogService.insertGeneralLog(new LogInfoPO(logInfo));
        } catch (Exception e) {
            log.error("保存日志异常！", e);
        }
    }

    @Override
    @PostMapping(value = "queryPassList")
    public LinkedResult queryPassList(Integer passType) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("查询通过信息接口 入参：{}", mapper.writeValueAsString(passType));
        }
        List<String> ret = null;

        try {
            switch (passType) {
                case 2:
                    ret = redisUtils.getRedisValueList(RedisKeysConfig.KEY_BLACKLIST, () -> passHandleService.queryBlackList());
                    break;
                case 1:
                    ret = redisUtils.getRedisValueList(RedisKeysConfig.KEY_WIHTELIST, passHandleService::queryWihteList);
                    break;
                default:
                    ret = null;
            }

        } catch (Exception e) {
            log.error("增加通过信息接口 异常", e);
            return LinkedResult.Error("未知异常，请联系管理员！");
        }
        return !CollectionUtils.isEmpty(ret) ? LinkedResult.Success(ret) : LinkedResult.OK(false, "未查询到数据");
    }

    @Override
    public Boolean addDeleteBak(LinkedDeleteBak deleteBak) {

        DeleteBakPO deleteBakPO = null;
        try {
            deleteBakPO = mapper.readValue(mapper.writeValueAsString(deleteBak), DeleteBakPO.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return deleteBakService.insertDeleteBak(deleteBakPO);
    }
}
